﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RioStudio.Website.Model;

namespace RioStudio.Website.View
{
    public partial class Service : RioPage
    {
        #region "Property"

        public string Slug { get; set; }

        #endregion

        private void GetData(string slug)
        {
            var model = tData.Instance.ExecuteReader<RioStudio.Website.Model.Service>("Proc_GetServiceBySlug", slug).FirstOrDefault();

            if (model != null)
            {
                literalDetail.Text = model.Detail;
                hiddenImageID.Value = model.ServiceID.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["slug".ToLower()] != null)
            {
                Slug = Request.Params["slug".ToLower()];
            }

            if (!IsPostBack)
            {
                GetData(this.Slug);

                rlvService.Rebind();
                rlvSampleAlbum.Rebind();
            }
        }

        protected void rlvService_NeedDataSource(object sender, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
        {
            try
            {
                rlvService.DataSource = tData.Instance.ExecuteReader<RioStudio.Website.Model.Service>("Proc_GetAllServiceByInactive", false);
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void rlvSampleAlbum_NeedDataSource(object sender, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
        {
            try
            {
                rlvSampleAlbum.DataSource = tData.Instance.ExecuteReader<RioStudio.Website.Model.Gallery>("Proc_GetAllGalleryIsFeature", true);
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void rlvComment_NeedDataSource(object sender, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
        {
            if (!string.IsNullOrEmpty(hiddenImageID.Value))
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    rlvComment.DataSource = tData.Instance.ExecuteReader<ServiceComment>("Proc_GetAllServiceCommentByServiceID", new Guid(hiddenImageID.Value), null);
                }
                else
                {
                    rlvComment.DataSource = tData.Instance.ExecuteReader<ServiceComment>("Proc_GetAllServiceCommentByServiceID", new Guid(hiddenImageID.Value), true);
                }
            }
        }

        protected void rlvComment_ItemCommand(object sender, Telerik.Web.UI.RadListViewCommandEventArgs e)
        {
            try
            {
                var _imageCommentID = Guid.Empty;
                var _imageComment = new ServiceComment();
                switch (e.CommandName)
                {
                    case "Remove":
                        _imageCommentID = new Guid(e.CommandArgument.ToString());
                        _imageComment.ServiceCommentID = _imageCommentID;
                        tData.Instance.ExecuteNonQuery<ServiceComment>("Proc_DeleteServiceComment", _imageComment);
                        break;
                    case "Approve":
                        _imageCommentID = new Guid(e.CommandArgument.ToString());
                        _imageComment.ServiceCommentID = _imageCommentID;
                        _imageComment.IsApproved = true;
                        tData.Instance.ExecuteNonQuery<ServiceComment>("Proc_ChangeServiceCommentStatus", _imageComment);
                        break;
                    case "Unapprove":
                        _imageCommentID = new Guid(e.CommandArgument.ToString());
                        _imageComment.ServiceCommentID = _imageCommentID;
                        _imageComment.IsApproved = false;
                        tData.Instance.ExecuteNonQuery<ServiceComment>("Proc_ChangeServiceCommentStatus", _imageComment);
                        break;
                }

                rlvComment.Rebind();
            }
            catch (Exception)
            {
                throw;
            }
        }



        protected void btnSend_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                var model = new ServiceComment();
                model.ServiceCommentID = Guid.NewGuid();
                model.ServiceID = new Guid(hiddenImageID.Value);
                model.Detail = txtDetail.Text;
                model.PostedName = txtFullname.Text;
                model.IsApproved = false;

                tData.Instance.ExecuteNonQuery<ServiceComment>("Proc_InsertServiceComment", model);

                txtDetail.Text = string.Empty;
                txtFullname.Text = string.Empty;

                rlvComment.Rebind();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}